<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/10/23
 * Time: 14:16
 */
namespace C\lib{
    class Ditu{
        /**
         * 从百度地图api以curl获取经纬度
         * 数组形式返回
         */
        public static function curlGetWeb($address_sender)
        {

            $Url="http://restapi.amap.com/v3/geocode/geo?address=".trim($address_sender)."&output=json&key=da4921d7765045197c3ff6248d2e71b9";
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $Url);
            curl_setopt($ch, CURLOPT_HEADER, false);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //如果把这行注释掉的话，就会直接输出
            $result=curl_exec($ch);
            curl_close($ch);
            $infolist=json_decode($result);
            //var_dump($infolist->geocodes[0]->location);exit;
            $locations=explode(',',$infolist->geocodes[0]->location);
            if(!empty($infolist->geocodes)){

                $array=array(
                    'lng'=>$locations[0],
                    'lat'=>$locations[1],
                );
                return $array;
            }
            else
            {
                return false;
            }

//            $Url="http://api.map.baidu.com/geocoder?address=".trim($address_sender)."&output=json&key=G1jfgVeOi0O6KGW2eddNf4LQ0zqw1Ny9";
//            $ch = curl_init();
//            curl_setopt($ch, CURLOPT_URL, $Url);
//            curl_setopt($ch, CURLOPT_HEADER, false);
//            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //如果把这行注释掉的话，就会直接输出
//            $result=curl_exec($ch);
//            curl_close($ch);
//            $infolist=json_decode($result);
//            $array=array();
//            if(isset($infolist->result->location) && !empty($infolist->result->location)){
//                //最后2位随机
////                $rand_num=rand(10,99);
////                $lng=substr($infolist->result->location->lng,0,7).$rand_num;
////                $lat=substr($infolist->result->location->lat,0,8).$rand_num;
//
//                $array=array(
//                    'lng'=>$infolist->result->location->lng,
//                    'lat'=>$infolist->result->location->lat,
//                );
//                return $array;
//            }
//            else
//            {
//                return false;
//            }
        }


        //百度坐标BD-09到火星坐标GCJ02(高德，谷歌，腾讯坐标)

//    define('X_PI',3.14159265358979324*3000.0/180.0);
        public static function bdTOgcj($lat,$lng){
           // echo 33;exit;
            if(!empty($lat) && !empty($lng)){
                $x = $lng - 0.0065;
                $y = $lat - 0.006;
                $z = sqrt($x * $x + $y * $y) - 0.00002 * sin($y * 3.14159265358979324*3000.0/180.0);
                $theta = atan2($y, $x) - 0.000003 * cos($x * 3.14159265358979324*3000.0/180.0);
                $lng = $z * cos($theta);
                $lat = $z * sin($theta);
                //后2位随机 误差为米
                $rand_num=rand(10,99);

                $lngw=substr($lng,0,8).$rand_num;
                $latw=substr($lat,0,7).$rand_num;
                $array=array(
                    'lng'=>$lngw,
                    'lat'=>$latw,
                );
                return $array;
            }else{
                return false;
            }
            //$to = new Coordinate();
        }


        //火星坐标(GCJ02坐标，高德，谷歌，腾讯坐标)到百度坐标BD-09
        public static function gcjTObd($lat,$lng){

            $x = $lng;
            $y = $lat;
            $z = sqrt($x * $x + $y * $y) + 0.00002 * sin($y * 3.14159265358979324*3000.0/180.0);
            $theta = atan2($y, $x) + 0.000003 * cos($x * 3.14159265358979324*3000.0/180.0);
            $lng = $z * cos($theta) + 0.0065;
            $lat = $z * sin($theta) + 0.006;
            //后2位随机 误差为米
            $rand_num=rand(10,99);

            $lngw=substr($lng,0,7).$rand_num;
            $latw=substr($lat,0,8).$rand_num;

            $lngw=substr($lng,0,7);
            $latw=substr($lat,0,8);
            $array=array(
                'lng'=>$lngw,
                'lat'=>$latw,
            );
            return $array;
        }


//    $from = new Coordinate(55.8271, 137.8345);
//
//    $ret = GeoTransUtil::wgsTOgcj($from);
//    echo '</br>';
//    echo $ret->x.','.$ret->y;

        /**
         * @desc 根据两点间的经纬度计算距离
         * @param float $lat 纬度值
         * @param float $lng 经度值
         */
        public static function getDistance($lat1, $lng1, $lat2, $lng2){
            $earthRadius = 6367000; //approximate radius of earth in meters
            $lat1 = ($lat1 * pi() ) / 180;
            $lng1 = ($lng1 * pi() ) / 180;
            $lat2 = ($lat2 * pi() ) / 180;
            $lng2 = ($lng2 * pi() ) / 180;
            $calcLongitude = $lng2 - $lng1;
            $calcLatitude = $lat2 - $lat1;
            $stepOne = pow(sin($calcLatitude / 2), 2) + cos($lat1) * cos($lat2) * pow(sin($calcLongitude / 2), 2);
            $stepTwo = 2 * asin(min(1, sqrt($stepOne)));
            $calculatedDistance = $earthRadius * $stepTwo;
            return round($calculatedDistance);
        }

    }
}